<?php

class Managepages extends CI_Controller {

    function __construct() {
        parent::__construct();
        $id = $this->session->userdata('id');
        $adminname = $this->session->userdata('adminname');
        $hash = $this->session->userdata('hash');
        if (!empty($hash) && $hash == md5(SECRETCODE . $id . $adminname . SECRETCODE)) {
            $this->admin['name'] = $adminname;
            $this->admin['id'] = $id;
            $this->admin['hash'] = $hash;
            $this->load->model('news_m');
        } else {
            redirect(base_url() . 'admin/login');
        }
        $this->load->model('page_m');
        $this->load->model('catalogue_m');
    }

    function index() {
        if ($this->admin['name'] != '' && $this->admin['id'] != '' && $this->admin['hash'] == md5(SECRETCODE . $this->admin['id'] . $this->admin['name'] . SECRETCODE)) {
            $this->load->library('pagination');
            $offset = $this->uri->segment(3);
            $limit = 12;
            $config['base_url'] = site_url('admin/managepages');
            $config['uri_segment'] = 3;
            $config['total_rows'] = $this->db->count_all('pages');
            $config['per_page'] = $limit;
            $this->pagination->initialize($config);
            $data['paginator'] = $this->pagination->create_links();

            $data['pages'] = $this->page_m->getall($limit, $offset, 0);

            $this->load->view('admin/common/header');
            $this->load->view('admin/pages', $data);
            $this->load->view('admin/common/footer');
        } else
            redirect(base_url() . 'admin/login');
    }

    function add() {
        if ($this->admin['name'] != '' && $this->admin['id'] != '' && $this->admin['hash'] == md5(SECRETCODE . $this->admin['id'] . $this->admin['name'] . SECRETCODE)) {
            if (isset($_POST['add']) && $_POST['add'] == 'Add') {


                $data['content'] = trim($this->input->post("contents"));
                $data['catalogue_id'] = $this->input->post('catalogue');
                $data['status'] = $this->input->post('status');

                $arr = array("\r\n", "\n", "\r");
                $data['content'] = str_replace($arr, '', $data['content']);

                //die(print_r($data));
                if (trim($data['content']) == '')
                    redirect(base_url() . 'admin/managepages/add');
                $id = $this->page_m->add($data);
                if ($id > 0)
                    redirect(base_url() . 'admin/managepages');
                else
                    die('Lỗi thêm tin mới!');
            }else {

                $data['catalogue'] = $this->catalogue_m->get_catalogue();

                $this->load->view('admin/common/header');
                $this->load->view('admin/page_add', $data);
                $this->load->view('admin/common/footer');
            }
        } else
            redirect(base_url() . 'admin/login');
    }

    function update($id) {
        if (!is_numeric($id) || $id < 0)
            redirect(base_url() . 'admin/managepages');

        if (isset($_POST['update']) && $_POST['update'] == 'Update') {
            $data['content'] = str_replace("\n", '', trim($this->input->post("contents"), "\n"));
            $data['catalogue_id'] = $this->input->post('catalogue');
            $data['status'] = $this->input->post('status');

            $arr = array("\r\n", "\n", "\r");
            $data['content'] = str_replace($arr, '', $data['content']);

            if (trim($data['content']) == '')
                redirect(base_url() . 'admin/managepages/update/' . $id);

            if ($this->page_m->update($id, $data))
                redirect(base_url() . 'admin/managepages');
            else
                die('Lỗi cập nhật tin');
        }else {
            //die('xxx');
            $data['page'] = $this->page_m->get_page($id);
            $data['catalogue'] = $this->catalogue_m->get_catalogue();

            $this->load->view('admin/common/header');
            $this->load->view('admin/page_edit', $data);
            $this->load->view('admin/common/footer');
        }
    }

    function delete($id) {
        if ($this->admin['name'] != '' && $this->admin['id'] != '' && $this->admin['hash'] == md5(SECRETCODE . $this->admin['id'] . $this->admin['name'] . SECRETCODE)) {
            if (!is_numeric($id))
                redirect(base_url() . 'admin/managepages');
            if ($this->page_m->delete($id))
                redirect(base_url() . 'admin/managepages');
            else
                die('Lỗi xóa tin');
        }
        else
            redirect(base_url() . 'admin/login');
    }

    function delete_page() {
        if ($this->admin['name'] != '' && $this->admin['id'] != '' && $this->admin['hash'] == md5(SECRETCODE . $this->admin['id'] . $this->admin['name'] . SECRETCODE)) {
            $ids = $this->input->post('selected');
            if (is_array($ids)) {
                if ($this->page_m->delete($ids))
                    redirect(base_url() . 'admin/managepages');
                else
                    die('Lỗi xóa tin');
            }else {
                redirect(base_url() . 'admin/managepages');
            }
        } else
            redirect(base_url() . 'admin/login');
    }

    function search() {
        if ($this->admin['name'] != '' && $this->admin['id'] != '' && $this->admin['hash'] == md5(SECRETCODE . $this->admin['id'] . $this->admin['name'] . SECRETCODE)) {
            if (isset($_POST['search']) && $_POST['search'] == 'Tìm kiếm') {

                $data['keyword'] = $this->input->post('txt_search');
                $this->session->set_userdata('keyword', $data['keyword']);

                if (trim($data['keyword']) == '')
                    redirect(base_url() . 'admin/managepages');

                $this->load->library('pagination');
                $offset = $this->uri->segment(4);
                $limit = 12;
                $config['base_url'] = site_url('admin/managepages/search');
                $config['uri_segment'] = 4;
                $config['total_rows'] = $this->page_m->count_for_search($data['keyword']);
                $config['per_page'] = $limit;
                $this->pagination->initialize($config);
                $data['paginator'] = $this->pagination->create_links();

                $data['pages'] = $this->page_m->search_result($data['keyword'], $limit, $offset);

                $this->load->view('admin/common/header');
                $this->load->view('admin/pages', $data);
                $this->load->view('admin/common/footer');
            }else {

                $data['keyword'] = $this->session->userdata('keyword');
                if (!isset($data['keyword']) || $data['keyword'] == '')
                    redirect(base_url() . 'admin/managepages');

                $this->load->library('pagination');
                $offset = $this->uri->segment(4);
                $limit = 12;
                $config['base_url'] = site_url('admin/managepages/search');
                $config['uri_segment'] = 4;
                $config['total_rows'] = $this->page_m->count_for_search($data['keyword']);
                $config['per_page'] = $limit;
                $this->pagination->initialize($config);
                $data['paginator'] = $this->pagination->create_links();

                $data['pages'] = $this->page_m->search_result($data['keyword'], $limit, $offset);

                $this->load->view('admin/common/header');
                $this->load->view('admin/pages', $data);
                $this->load->view('admin/common/footer');
            }
        } else
            redirect(base_url() . 'admin/login');
    }

}